#include "LogicEngine.h"

	LogicEngine::LogicEngine(int32 id):BaseEntity(id),
		Component("LogicEngine"),
		m_screenWidth(WIDTH),
		m_screenHeight(HEIGHT)
	{
		m_pStateMachine = new StateMachine<LogicEngine>(this);
		m_pStateMachine->SetCurrentState(MenuState::Instance());
		m_eGState=FIGHT;
		m_eFState=FBROSE;
		m_bFight=true;
	}

	LogicEngine::~LogicEngine(void)
	{ 
		delete m_pStateMachine;
	}

	void LogicEngine::addedToObject()
	{
		requestComponent("Camera",&LogicEngine::SetCamera);
		requestMessage("Key0Pushed",&LogicEngine::Esc);
		requestMessage("Key15Pushed",&LogicEngine::KeyQ);
		requestMessage("Key16Pushed",&LogicEngine::KeyW);
		requestMessage("Key17Pushed",&LogicEngine::KeyE);
		requestMessage("Key29Pushed",&LogicEngine::KeyA);
		requestMessage("Key30Pushed",&LogicEngine::KeyS);
		requestMessage("ChangeToFightState",&LogicEngine::InitFight);
		requestMessage("Key31Pushed",&LogicEngine::KeyD);
		requestMessage("MouseLeftClicked",&LogicEngine::MLClicked);
		requestMessage("MouseRightClicked",&LogicEngine::MRClicked);
	}

	void LogicEngine::MLClicked(const Message &msg)
	{
		vector<boost::any> any;
		any.push_back(MOUSE_LEFT_CLICKED);
		any.push_back(m_mousePoint);
		m_pStateMachine->CurrentState()->OnMessage(this,&any);
	}

	void LogicEngine::MRClicked(const Message &msg)
	{
		vector<boost::any> any;
		any.push_back(MOUSE_RIGHT_CLICKED);
		any.push_back(m_mousePoint);
		m_pStateMachine->CurrentState()->OnMessage(this,&any);
	}

	void LogicEngine::ChangeFightState(FIGHT_STATE fs)
	{
		m_eFState=fs;
	}

	void LogicEngine::KeyQ(const Message & msg)
	{
		sendMessage("RowRight");
		sendMessage("UpdateCursor",m_mousePoint);

	}

	void LogicEngine::KeyW(const Message & msg)
	{
		sendMessage("WalkFront");
		sendMessage("UpdateCursor",m_mousePoint);
	}

	void LogicEngine::KeyE(const Message & msg)
	{
		sendMessage("RowLeft");
		sendMessage("UpdateCursor",m_mousePoint);
	}

	void LogicEngine::KeyA(const Message & msg)
	{
		sendMessage("ShiftRight");
		sendMessage("UpdateCursor",m_mousePoint);
	}

	void LogicEngine::KeyS(const Message & msg)
	{
		sendMessage("WalkBack");
		sendMessage("UpdateCursor",m_mousePoint);
	}

	void LogicEngine::KeyD(const Message & msg)
	{
		sendMessage("ShiftLeft");
		sendMessage("UpdateCursor",m_mousePoint);
	}

	GAME_STATE LogicEngine::GetGameState()
	{
		return m_eGState;
	}

	FIGHT_STATE LogicEngine::GetFightState()
	{
		return m_eFState;
	}

	void LogicEngine::SetCamera(const Message & msg)
	{
		//Camera* c=(Camera*)msg.sender;
		//c->ResetCamera(m_screenWidth,m_screenHeight);
	}

	void LogicEngine::Esc(const Message & msg)
	{
		exit(0);
	}

	void LogicEngine::Update()
	{
		m_pStateMachine->Update();
		sendMessage("Update");
	}

	void LogicEngine::InitFight(const Message &msg)
	{
		//Pickup* pick=new Pickup;
		//ObjectId pId=createObject();
		//addComponent(pId,pick);
		Figure* figure=new Figure;
		ObjectId fId=createObject();
		addComponent(fId,figure);
        FigureManager* fm=new FigureManager;
		ObjectId fmId=createObject();
		addComponent(fmId,fm);
        ModelManager* mm=new ModelManager;
        ObjectId mmId=createObject();
        addComponent(mmId,mm);
		//World* world=new World;
		//ObjectId wId=createObject();
		//addComponent(wId,world);
		sendMessage("InitFight");
		sendMessage("PutFigure");
	}

	D3DXVECTOR2 LogicEngine::GetMousePoint()
	{
		return m_mousePoint;
	}

	void LogicEngine::SetMousePoint(int32 x, int32 y)
	{
		m_mousePoint.x=x+m_screenWidth/2;
		m_mousePoint.y=y+m_screenHeight/2;
		
		if(m_mousePoint.x<0)
			m_mousePoint.x=0;
		if(m_mousePoint.y<0)
			m_mousePoint.y=0;
		if(m_mousePoint.x>m_screenWidth-32)
			m_mousePoint.x=m_screenWidth-32;
		if(m_mousePoint.y>m_screenHeight-32)
			m_mousePoint.y=m_screenHeight-32;
		sendMessage("UpdateCursor",m_mousePoint);
	}

	void LogicEngine::ChangeGameState(GAME_STATE gs)
	{
		if(gs==m_eGState)
			return;
		switch(gs)
		{
		case MENU:
			m_pStateMachine->ChangeState(MenuState::Instance());
			break;
		case STORY:
			m_pStateMachine->ChangeState(StoryState::Instance());
			break;
		case FIGHT:
			m_pStateMachine->ChangeState(FightState::Instance());
			break;
		case END:
			break;
		default:
			break;
		}
		m_eGState=gs;
	}